<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.xh')"
            >
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.name')"
            >
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.stuCate')"
              >
                <a-select allowClear v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.pydw')"
              >
                <a-select allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.zy')"
              >
                <a-select v-model="queryParam.sszym" allowClear showSearch :filterOption="filterOption">
                  <a-select-option v-for="(i,index) in selectMenu.zy" :key="index" :value="i.zyh">{{ i.zyh+ '- ' +i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.nj')"
              >
                <a-select allowClear v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                      :label="$t('key.xjzt')"
              >
                <a-select allowClear v-model="queryParam.xjztm">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">{{ $t('button.query') }}</a-button>
            <a-button type="primary" style="margin:0 10px" @click="batchReset">{{ $t('menu.trainingPlanReset') }}</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
        <a-alert style="margin-bottom: 10px;" message="温馨提示：本功能可以实现将已审核状态还原为未提交状态，以便学生重新修改培养计划再次提交" banner />
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :rowSelection="options.rowSelection"
      :alert="{show: false}"
      :pagination="{ pageSize: 10 }"
      :scroll="{x:1200}"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      
      <template slot="operation" slot-scope="a,b">
        <a @click="openDetail(b)">{{$t('button.showStu')}}</a>
      </template>
    </s-table>

    <review-detail ref="openDetail" :visible="checkVisible" @cancel="checkVisible = false" />
  </a-card>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import reviewDetail from '../components/reviewDetail'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {
        // xjztm: '1'
      },
      columns: [
        {
          title: this.$t('key.no'),
          width: '80px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.xh'),
          width: '120px',
          dataIndex: 'xh'
        },
        {
          title: this.$t('key.name'),
          width: '120px',
          dataIndex: 'xm'
        },
        {
          title: this.$t('key.sex'),
          width: '100px',
          dataIndex: 'xbmc'
        },
        {
          title: this.$t('key.nj'),
          width: '100px',
          dataIndex: 'sznjmc'
        },
        {
          title: this.$t('key.pydw'),
          width: '160px',
          dataIndex: 'dwmc'
        },
        {
          title: this.$t('key.zy'),
          width: '160px',
          dataIndex: 'zymc'
        },
        {
          title: this.$t('key.stuCate'),
          width: '120px',
          dataIndex: 'xslbmc'
        },
        {
          title: this.$t('key.mentor'),
          width: '120px',
          dataIndex: 'dsxm'
        },
        {
          title: this.$t('key.shzt'),
          dataIndex: 'pyjhztmc',
          width: '160px'
        },
        {
          title: this.$t('key.option'),
          width: '100px',
          scopedSlots: { customRender: 'operation' },
          fixed:'right'
        }
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.pyjhResetList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      selectedRows: [],
      options: {
        rowSelection: {
          onChange: this.onSelectChange
        }
      },
      checkVisible: false,
      isEdit: false
    }
  },
  components: {STable, reviewDetail},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0073: state => state.app['TYPE0073'], // 学籍状态
    })
  },
  beforeRouteEnter(to, form, next){
    console.log(to)
    next(vm => {
      console.log(vm)
      vm.$store.dispatch('app/sysInfo', true).then(data => {
      if ( +new Date() > +data.pyjhshjssj  ||  +new Date() < +data.pyjhshkssj) {
        vm.$confirm({
          title: '当前不在培养计划审核时间范围，无法进行操作',
          centered: true,
          okText: '确认',
          cancelText: '返回',
          onOk: () => {
            vm.$router.back()
          },
          onCancel: () => {
            vm.$router.back()
          }
        })
      }
    });
    })
  },
  created () {
    

    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
    },
    batchReset () {
      if (!this.selectedRows.length) {
        this.$message.warning('请选择需要重置的记录')
        return
      }
      this.$confirm({
        title: '确定重置所选中的记录?',
        content: '当您点击确定按钮后，这些记录将会被重置',
        centered: true,
        onOk: () => {
          let ids = []
          this.selectedRows.forEach(plan => {
            ids.push(plan.id)
          })
          this.$api.cultivate.pyjhReset({ids: ids.join(',')}).then(res => {
            this.$message.success(res.msg)
            this.selectedRows = []
            this.$refs.table.refresh(true)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    openDetail(b) {
      this.checkVisible = true
      this.$refs.openDetail.getInfo(b.id, this.isEdit)
    }
  }
}
</script>

<style>

</style>
